package com.ripplezjw.blog.Service;

import com.ripplezjw.blog.mapper.ClassificationMapper;
import com.ripplezjw.blog.po.Article;
import com.ripplezjw.blog.po.Classification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ClassificationService {
    @Autowired
    ClassificationMapper classificationMapper;
    public String getClsNameByArticleId(int articleId) {
        return classificationMapper.findClsNameByArticleId(articleId);
    }

    public List<Classification> showAllCls() {
        return classificationMapper.findClassifications();
    }

    public String addClassification(Classification classification) {
        if (StringUtils.isEmpty(classification.getClsName())) return "分类名不能为空";
        if (classificationMapper.cntClassificationByClsName(classification.getClsName()) > 0) return "分类名已存在";
        classificationMapper.addClassifications(classification);
        return "";
    }

    public void clickDelUserBtn(int clsId) {
        classificationMapper.delClassificationByClsId(clsId);
    }

    public Classification clickModUserBtn(int clsId) {
        return classificationMapper.findClassificationById(clsId);
    }

    public String modUser(Classification classification) {
        if (StringUtils.isEmpty(classification.getClsName())) return "分类名不能为空";
        if (classificationMapper.cntClassificationByClsName(classification.getClsName()) > 0) return "分类名已存在";
        classificationMapper.updateClassificationByClsId(classification);
        return "";
    }

    public List<Article> getAllArticleIdByClsId(int clsId) {
        return classificationMapper.findArticleIdByClsId(clsId);
    }
}
